package com.xx.chb01.utils.json;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.*;
import java.io.IOException;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;

public class JsonUtils {

    public static SerializeFilter[] serializeFilters;
    public static SerializerFeature[] serializerFeature;
    public static SerializeConfig serializeConfig;

    static {

        /**
         * 对序列化的Long类型进行特殊处理,避免位数过大导致和js精度的丢失,只用于向页面发送json数据时使用
         */
        ObjectSerializer longSerializer = new ObjectSerializer() {
            public void write(JSONSerializer serializer, Object object, Object fieldName, Type type, int i) throws IOException {
                SerializeWriter out = serializer.getWriter();
                if ( object == null ) {
                    if ( out.isEnabled(SerializerFeature.WriteNullNumberAsZero) ) {
                        out.write('0');
                    } else {
                        out.writeNull();
                    }
                    return;
                }
                out.writeString(object.toString());
            }
        };

        ValueFilter numberFilter = new ValueFilter() {
            public Object process(Object object, String name, Object value) {
                if(value!=null){
                    if(value instanceof Long){
                        return value.toString();
                    } else  if(value instanceof BigInteger){
                        return value.toString();
                    }else  if(value instanceof BigDecimal){
                        return NumUtil.toString((BigDecimal) value);
                    }
                }
                return value;
            }
        };

        serializeFilters=new SerializeFilter[]{numberFilter};

        serializerFeature = new SerializerFeature[]{SerializerFeature.WriteMapNullValue, SerializerFeature.QuoteFieldNames, SerializerFeature.WriteNullStringAsEmpty,SerializerFeature.WriteEnumUsingToString};

        serializeConfig = new SerializeConfig();
        serializeConfig.put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd HH:mm:ss"));
        serializeConfig.put(Calendar.class, new SimpleDateFormatSerializer("yyyy-MM-dd HH:mm:ss"));
        serializeConfig.put(java.sql.Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd HH:mm:ss"));
        serializeConfig.put(java.sql.Timestamp.class, new SimpleDateFormatSerializer("yyyy-MM-dd HH:mm:ss"));
        serializeConfig.put(Long.class,longSerializer);
        serializeConfig.put(BigInteger.class,longSerializer);
    }

    public static String toJSONString(Object obj) {
        return JSON.toJSONString(obj, serializeConfig, serializeFilters, serializerFeature);
    }

    public static JSONObject toJSONObject(Map map) {
        return  new JSONObject(map);
    }

    public static  Map<String, String> toMap(JSONObject jsonObject) {
        return jsonObject.toJavaObject(new TypeReference<Map<String, String>>() {
        });
    }


    public static JSONObject toJSONObject(String jsonStr) {
        return JSON.parseObject(jsonStr);
    }
    public static JSONArray toJSONArray(String jsonStr) {
        return JSON.parseArray(jsonStr);
    }
    public static <T> T toObject(String jsonStr, Class<T> clazz){
        return JSON.parseObject(jsonStr,(Type) clazz);
    }
    public static <T> List<T> toArray(String jsonStr, Class<T> clazz){
        return JSON.parseArray(jsonStr, clazz);
    }
    public static <T> List<T> toArray(JSONArray jsonArray, Class<T> clazz){
        List<T> list=new ArrayList<T>();
        for(int i=0;i<jsonArray.size();i++){
            JSONObject jsonObject=jsonArray.getJSONObject(i);
            list.add(jsonObject.toJavaObject(clazz));
        }
        return list;
    }
}
